<template>
  <div>
    <el-container style="border: 18px solid #eee">
      <el-header style="text-align: right; font-size: 12px" height="20px">
        <el-input
          v-model="kwInput"
          size="mini"
          placeholder="输入关键词搜索"
          style="width: 40%;float: right"
          clearable
          class=""
          @keyup.native.enter="handleSearch"
        >
          <el-button
            slot="append"
            size="mini"
            style="width: 180px"
            type="success"
            icon="el-icon-search"
            @click="handleSearch"
          >搜索
          </el-button>
        </el-input>
        <div class="btn-group" style="float: right">
          <el-button size="mini" type="primary" @click="handleAction('create')">添加</el-button>
        </div>
      </el-header>
      <el-main>
        <el-table
          v-loading.body="listLoading"
          :data="tableData"
          border
          style="width: 100%"
        >
          <el-table-column
            prop="region"
            label="region"
            width="300px"
          />
          <el-table-column
            prop="shopid"
            label="shopid"
          />
          <el-table-column
            prop="account"
            label="account"
          />
          <el-table-column
            prop="vps_ip"
            label="vps_ip"
            width="180px"
          />
          <el-table-column
            prop="vm_ip"
            label="vm_ip"
            width="180px"
          />
          <el-table-column
            prop="create_at"
            label="创建日期"
          />
          <el-table-column align="center" label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="mini" type="success" @click="handleAction('edit', scope.row)">修改</el-button>
              <el-button size="mini" type="danger" @click="handleAction('delete', scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-pagination
        v-if="tableData.length"
        background
        class="mt-pagination"
        :current-page="page"
        :page-sizes="[10,50,100,200,300,500]"
        :page-size="pagesize"
        layout="total"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      />
    </el-container>
    <el-dialog
      :title="title"
      :visible.sync="innerDialogVisible"
      width="60%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="region">
          <el-select v-model="form.region" tyle="width: 100%" filterable clearable placeholder="选择账号区域">
            <el-option v-for="item in regions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="shopid">
          <el-input v-model="form.shopid" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="account">
          <el-input v-model="form.account" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="password">
          <el-input v-model="form.password" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="cookies">
          <el-input v-model="form.cookies" type="textarea" :rows="4" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="vps_ip">
          <el-input v-model="form.vps_ip" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="vm_ip">
          <el-input v-model="form.vm_ip" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="innerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Get, Post, Put, Delete } from '@/api/shopee/shopeeaccount'
import qs from 'qs'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}

export default {
  data() {
    return {
      action: '',
      listLoading: false,
      kwInput: '',
      order_by: '',
      order_desc: '',
      // pagination
      pagesize: 10,
      page: 1,
      total: 0,
      tableData: [],
      regions: [{
        label: '新加坡站点',
        value: 'SG'
      },
      {
        label: '马来西亚站点',
        value: 'MY'
      }
      ],
      form: {
        id: '',
        shopid: '',
        region: 'SG',
        account: '',
        password: '',
        cookies: '',
        vps_ip: '',
        vm_ip: ''
      },
      innerDialogVisible: false,
      title: '添加cookies'
    }
  },
  created() {
    this.getList()
  },
  methods: {
    resetTemp() {
      this.form = {
        id: '',
        region: 'SG',
        shopid: '',
        account: '',
        password: '',
        cookies: '',
        vps_ip: '',
        vm_ip: ''
      }
    },
    onSubmit() {
      Post(this.form).then((response) => {
        this.form.account = ''
        this.innerDialogVisible = false
        this.getList()
      }).catch(error => {
        console.log(error)
      })
    },
    getList() {
      this.listLoading = true
      Get(null,
        qs.stringify({
          kw: this.kwInput,
          pagesize: this.pagesize,
          page: this.page,
          order_by: this.order_by,
          order_desc: this.order_desc
        })).then((response) => {
        this.tableData = response.data.data
        this.total = response.data.total
        this.listLoading = false
      }).catch(error => {
        console.log(error)
        this.listLoading = false
      })
    },
    handleAction(action, row) {
      this.action = action
      switch (action) {
        case 'create':
          this.width = '50%'
          this.title = row
          this.resetTemp()
          this.innerDialogVisible = true
          break
        case 'delete':
          this.width = '30%'
          this.title = `确认要删除${row.account}?`
          this.form = Object.assign({}, row)
          this.innerDialogVisible = true
          break
        case 'edit':
          this.width = '50%'
          this.title = `修改账号${row.account}`
          this.form = Object.assign({}, row)
          console.log(this.form, row)
          this.innerDialogVisible = true
          break
      }
    },
    confirm() {
      const _data = this.form
      switch (this.action) {
        case 'create':
          Post(this.form
          ).then(response => {
            this.innerDialogVisible = false
            this.getList()
          })
          break
        case
          'delete'
          :
          Delete(_data.id).then((response) => {
            this.innerDialogVisible = false
            this.getList()
          })
          break
        case
          'edit'
          :
          Put(_data.id, {
            id: _data.id,
            region: _data.region,
            account: _data.account,
            shopid: _data.shopid,
            password: _data.password,
            vps_ip: _data.vps_ip,
            vm_ip: _data.vm_ip,
            cookies: _data.cookies
          }).then((response) => {
            this.innerDialogVisible = false
            this.$emit('reload')
            this.getList()
            this.notify('更新成功')
          })
          break
      }
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    handleSearch() {
      this.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    }
  }

}
</script>

<style scoped>

</style>
